/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;

using Gtk;
using Mono.Unix;

using DCSharp.Backend.Objects;

namespace DCSharp.Gui
{
	// NOTE: Any model used with this class must contains the following
	// columns.
	public enum TransferStoreColumn
	{
		Object,
		Name,
		Directory,
		SizeText,
		Size,
		Position,
		ProgressText,
		Progress,
		TimeLeft
	};

	public class TransferView : ExtendedTreeView
	{
		public TransferView(TreeModel model) : base(model)
		{
			// Columns
			TreeViewColumn column;
			CellRendererPixbuf pixbufRenderer;
			CellRendererText textRenderer;
			CellRendererProgress progressRenderer;

			// Name
			column = new TreeViewColumn();
			column.Title = Catalog.GetString("Name");
			column.Expand = true;
			column.Resizable = true;
			column.SortColumnId = (int)TransferStoreColumn.Name;
			AppendColumn(column);

			pixbufRenderer = new CellRendererPixbuf();
			column.PackStart(pixbufRenderer, false);
			column.SetCellDataFunc(pixbufRenderer, SetPixbuf);

			textRenderer = new CellRendererText();
			textRenderer.Ellipsize = Pango.EllipsizeMode.End;
			column.PackStart(textRenderer, true);
			column.AddAttribute(textRenderer, "text",
				(int)TransferStoreColumn.Name);
			column.SetCellDataFunc(textRenderer, SetTextStyle);

			ExpanderColumn = column;

			// Size
			textRenderer = new CellRendererText();
			column = AppendColumn(Catalog.GetString("Size"), textRenderer,
				"text", (int)TransferStoreColumn.SizeText);
			column.SortColumnId = (int)TransferStoreColumn.Size;
			column.SetCellDataFunc(textRenderer, SetTextStyle);

			// Status
			textRenderer = new CellRendererText();
			textRenderer.Ellipsize = Pango.EllipsizeMode.End;
			column = AppendColumn(Catalog.GetString("Status"), textRenderer);
			column.Resizable = true;
			column.MinWidth = 100;
			column.SetCellDataFunc(textRenderer, SetStatusText);

			// Progress
			progressRenderer = new CellRendererProgress();
			column = AppendColumn(Catalog.GetString("Progress"), progressRenderer);
			column.Resizable = true;
			column.AddAttribute(progressRenderer, "text",
				(int)TransferStoreColumn.ProgressText);
			column.AddAttribute(progressRenderer, "value",
				(int)TransferStoreColumn.Progress);
			column.SortColumnId = (int)TransferStoreColumn.Progress;

			// Time left
			textRenderer = new CellRendererText();
			column = AppendColumn(Catalog.GetString("Time Left"), textRenderer,
				"text", (int)TransferStoreColumn.TimeLeft);
			column.SortColumnId = (int)TransferStoreColumn.TimeLeft;
			column.SetCellDataFunc(textRenderer, SetTextStyle);
		}

		#region Methods

		protected virtual void SetPixbuf(TreeViewColumn column,
			CellRendererPixbuf pixbufRenderer, TreeModel model, TreeIter iter,
			object obj)
		{
		}

		protected virtual void SetStatusText(TreeViewColumn column,
			CellRendererText textRenderer, TreeModel model, TreeIter iter,
			object obj)
		{
		}

		protected virtual void SetTextStyle(TreeViewColumn column,
			CellRendererText textRenderer, TreeModel model, TreeIter iter,
			object obj)
		{
		}

		private void SetPixbuf(TreeViewColumn column, CellRenderer renderer,
			TreeModel model, TreeIter iter)
		{
			object obj = model.GetValue(iter, (int)TransferStoreColumn.Object);

			SetPixbuf(column, (CellRendererPixbuf)renderer, model, iter, obj);
		}

		private void SetStatusText(TreeViewColumn column, CellRenderer renderer,
			TreeModel model, TreeIter iter)
		{
			object obj = model.GetValue(iter, (int)TransferStoreColumn.Object);

			SetStatusText(column, (CellRendererText)renderer, model, iter, obj);
		}

		private void SetTextStyle(TreeViewColumn column, CellRenderer renderer,
			TreeModel model, TreeIter iter)
		{
			object obj = model.GetValue(iter, (int)TransferStoreColumn.Object);

			SetTextStyle(column, (CellRendererText)renderer, model, iter, obj);
		}

		#endregion
	}
}
